package com.nsyue.auth.exception;

import com.nsyue.auth.controller.result.ApiStatusEnum;
import com.nsyue.auth.controller.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @description @ControllerAdvice + @ExceptionHandler +自定义异常  来做全局异常处理
 *
 * @author laixm
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 拦截自定义异常NsyueAuthException
     *
     * @param e
     * @return ResponseEntity
     */
    @ExceptionHandler(NsyueAuthException.class)
    public ResponseEntity<Result<?>> handlerNsyueAuthException(NsyueAuthException e) {
        log.error(e.getErrorMessage(), e);
        return ResponseEntity.badRequest().body(Result.failed(e.getMessage()));
    }

    @ExceptionHandler(UnsupportedGrantTypeException.class)
    public ResponseEntity<Result<?>> handlerUnsupportedGrantTypeException(UnsupportedGrantTypeException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.badRequest().body(Result.failed(ApiStatusEnum.UNSUPPORTED_GRANT_TYPE));
    }

    @ExceptionHandler(InvalidTokenException.class)
    public ResponseEntity<Result<?>> handlerUnsupportedGrantTypeException(InvalidTokenException e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.badRequest().body(Result.failed(ApiStatusEnum.INVALID_TOKEN));
    }

    /*@ExceptionHandler(Exception.class)
    public ResponseEntity<Result<?>> handlerException(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.badRequest().body(Result.failed(e.getMessage()));
    }*/
}

